﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL.Business;

public partial class Order_edit : System.Web.UI.Page
{
    BLL.Business.OrderBLL orderBLL = new OrderBLL();
    BLL.Business.UserBLL userBLL = new UserBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
            DataBindOrder();
    }
    // Gridview绑定数据
    public void DataBindOrder()
    {
        try
        {
            String UserName = Session["UserName"].ToString();
            int UserId = Int32.Parse((userBLL.SelectByName(UserName)).Tables[0].Rows[0]["User_Idint"].ToString());

            DataSet ds = orderBLL.SelectSecondOrderByUserId(UserId);
            if ((ds == null) ||
                (ds.Tables.Count == 0) ||
                (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
            }
            else
            {
                ds.Tables[0].Columns.Add("orderStates");  // 添加新列（表示订单状态）
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["Ord_IsCheckedint"].ToString() == "0")
                    {
                        dr["orderStates"] = "没审批";
                    }
                    else if (dr["Ord_IsCheckedint"].ToString() == "1")
                    {
                        dr["orderStates"] = "已审批";
                    }
                    else
                    {
                        dr["orderStates"] = "已结款";
                    }
                }
                orderList.DataSource = ds;
                orderList.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 处理删除事件
    protected void orderList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteOrder")
        {
            try
            {
                int orderId = Int32.Parse(e.CommandArgument.ToString());
                if (orderBLL.DeleteOrder(orderId))
                {
                    DataBindOrder();
                }
                else
                {
                    Response.Write("<script>alter('出现错误，请检查！')</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
            }
        }
    }
}
